package com.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.dto.MerchantOrders;
import com.community.dto.SupplierOrders;
import com.community.pojo.*;
import com.community.service.ISupplierService;
import com.community.utils.PageUtils;
import com.github.pagehelper.PageInfo;
import com.community.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/community/supplier")
public class SupplierController {
    @Autowired
    private ISupplierService iSupplierService;

    /**
     * 供应商查询所有
     * @param pageNo
     * @param pageSize
     * @param supplier
     * @return
     */
    @RequestMapping("/queryAll/{pageNo}/{pageSize}")
    @ResponseBody
    public PageInfo<Supplier> page(@PathVariable int pageNo,
                                       @PathVariable int pageSize,
                                       @RequestBody Supplier supplier) {
        PageInfo<Supplier> pageInfo=this.iSupplierService.getSupplierList(pageNo,pageSize,
                supplier);
        System.out.println("pageInfo = " + pageInfo);
        return pageInfo;
    }

    /**
     *获取id
     * @param
     * @return
     */
    @RequestMapping("/getById/{supplierId}")
    @ResponseBody
    public Supplier getById(int merchantId) {
        Supplier supplier = iSupplierService.getById(merchantId);
        return supplier;
    }

    /**
     * 修改
     * @param supplier
     * @return
     */

    @RequestMapping("/updById")
    @ResponseBody
    public String updById(@RequestBody Supplier supplier) {
        boolean flag = iSupplierService.updateById(supplier);
        if (flag) {
            return "ok";
        }
        return "error";
    }

    /**
     *删除获取id
     * @param supplier
     * @return
     */
    @RequestMapping("/delBySupplierId")
    @ResponseBody
    public String delBackUser(@RequestBody Supplier supplier) {
        iSupplierService.delBySupplierId(supplier);
        return "删除成功！";
    }

    /**
     *条件查询
     * @param supplier
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/supplierPage/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<Supplier> supplierFuHePage(
            @RequestBody Supplier supplier,
            @PathVariable int pageNo,
            @PathVariable int pageSize) {
        System.out.println("supplier = " + supplier);
        PageUtils<Supplier> page = this.iSupplierService.supplierFuHePage(pageNo, pageSize,supplier);
        return page;
    }


    /**
     * 复核成功
     * @param
     * @return
     */
    @RequestMapping("/supplierFuHeOk")
    @ResponseBody
    public String storageCellReviewPassed(@RequestBody int id) {
        Supplier supplier=this.iSupplierService.getById(id);
        supplier.setCheckTag("S001-3");
        iSupplierService.updateById(supplier);

        return "ok";
    }
    /**
     * 复核不成功
     * @param supplier
     * @return
     */
    @RequestMapping("/supplierFuHeNoOk")
    @ResponseBody
    public String storageCellNoReviewPassed(@RequestBody Supplier supplier) {
        supplier.setCheckTag("S001-2");
        boolean flag = iSupplierService.updateById(supplier);
        if (flag) {
            return "correct";
        }
        return "error";
    }
    /**
     * 复核按钮按照id查所有
     * @param id
     * @return
     */
    @RequestMapping("/SupplierDataPage/{id}")
    @ResponseBody
    public Supplier SupplierDataPage(@PathVariable int id) {
        Supplier supplier = this.iSupplierService.getById(id);
        return supplier;
    }

    //分页查询所有供应商
    @RequestMapping("/list")
    @ResponseBody
    public PageUtils<Supplier> queryAll(int pageNo, int pageSize){
        return this.iSupplierService.searchAll(pageNo,pageSize);
    }

    /**
     * 2.查询用户
     * @param name
     * @return
     */
    @ResponseBody
    @RequestMapping("/getConsumerByLoginName")
    public Supplier getConsumerByLoginName(@RequestBody String name) {
        // 可能去除可能存在的等号字符
        name = name.replace("=", "");
        //根据log_name获取对象GetOne
        return iSupplierService.getOne(new QueryWrapper<Supplier>().eq("name", name));
    }
    /**
     * 3.根据用户id查询商户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getMerchantByID/{id}")
    public Object getMerchantByID(@PathVariable Integer id) {
        return iSupplierService.getOne(new QueryWrapper<Supplier>().eq("supplier_id",id));
    }//1.根据用户id，查询商户所有的订单
    @ResponseBody
    @RequestMapping("/supplierBySupplierId/{id}")
    public List<SupplierOrders> supplierBySupplierId(@PathVariable Integer id) {
        return iSupplierService.supplierBySupplierId(id);
    }
    /**
     * 供应商表查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/show/{currentPage}/{pageSize}")
    public PageUtils<Supplier> show(@PathVariable int currentPage,
                                  @PathVariable  int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Supplier> list = iSupplierService.list();
        PageInfo<Supplier> p = new PageInfo<Supplier>(list);
        PageUtils<Supplier> utils = new PageUtils<Supplier>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
    /**
     * 根据id查询所有供应商的
     * @param supplierId
     * @return
     */
    @RequestMapping("/showById")
    @ResponseBody
    public Supplier showById(Integer supplierId) {
        Supplier byId = iSupplierService.getById(supplierId);
        return byId;
    }
    /**
     * 添加新供应商
     * @param supplier
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Boolean add(@RequestBody Supplier supplier) {
        if (supplier.getStatus() == null || supplier.getStatus().isEmpty()) {
            supplier.setStatus("C001-1");
        }
        if (supplier.getCheckTag() == null || supplier.getCheckTag().isEmpty()) {
            supplier.setCheckTag("S001-1");
        }
        boolean save = iSupplierService.save(supplier);
        return save;
    }
}
